<html>
<head><meta charset="utf-8"><title>Using a non-system allocator for rustc on macOS · t-compiler/performance · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/index.html">t-compiler/performance</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html">Using a non-system allocator for rustc on macOS</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225572350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/225572350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#225572350">(Feb 08 2021 at 16:47)</a>:</h4>
<p>In <a href="https://github.com/purpleprotocol/mimalloc_rust/issues/41">https://github.com/purpleprotocol/mimalloc_rust/issues/41</a> there's some discussion about overriding malloc on macOS for use with rustc, and in particular the implication is that apparently you believe that you use jemalloc's overriding on mac (you <a href="https://github.com/gnzlbg/jemallocator/blob/master/jemalloc-sys/build.rs#L45">don't</a>, and likely can't).</p>
<p>The performance of macOS's system allocator (well, <code>free</code> really) is atrocious, mostly because it makes some guarantees about <code>free(p)</code> for invalid <code>p</code> being safe and fine (for example, <code>free(p)</code> has to be able to handle pointers allocated out of a custom zone, and has to walk all the zones checking if that pointer belongs to them), which the system libraries <a href="https://github.com/opensource-apple/objc4/blob/master/runtime/hashtable2.mm#L68">do take advantage of</a>.</p>
<p>This is also why it's so hard to override malloc on macOS — your malloc has to either support, or sufficiently replace the zone system so that the system libraries don't hit these cases, and if you do so, you must do it early enough that it's before any system libraries allocate pointers — difficult because dyld has similar issues. (<code>mimalloc</code> has a mode that does try to override the zone system sufficiently, but it doesn't work as a static library, and requires setting DYLD_FORCE_FLAT_NAMESPACE in the env which often breaks other things).</p>
<p>Anyway, I had assumed that it was known that on macOS you were using the system allocator, and not jemalloc. I had believed y'all knew that you weren't doing this, mostly because overriding malloc on macOS is fraught with peril as mentioined above. I give some vague suggestions on how it can be done there, but <em>just</em> setting a <code>#[global_allocator]</code> would probably still help a lot.</p>
<p>Note: I'm not suggesting switching to mimalloc, you could continue to use jemalloc and set a <code>#[global_allocator]</code> of course, or even do the steps I describe <a href="https://github.com/purpleprotocol/mimalloc_rust/issues/41#issuecomment-775256749">here</a> with jemalloc</p>
<p>(EDIT: I guess maybe it would be good to switch to mimalloc also given these numbers: <a href="https://github.com/rust-lang/rust/pull/81782">https://github.com/rust-lang/rust/pull/81782</a>, but that's a different conversation)</p>



<a name="225591970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/225591970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#225591970">(Feb 08 2021 at 19:04)</a>:</h4>
<blockquote>
<p>apparently you believe that you use jemalloc's overriding on mac (you don't, and likely can't).</p>
</blockquote>
<p>Who is "you" in that sentence? The compiler team?</p>



<a name="225592078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/225592078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#225592078">(Feb 08 2021 at 19:05)</a>:</h4>
<p>Okay, I guess it is, based on the comment thread on <a href="https://github.com/purpleprotocol/mimalloc_rust/issues/41#issuecomment-775271617">the linked issue</a></p>



<a name="225592538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/225592538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#225592538">(Feb 08 2021 at 19:08)</a>:</h4>
<p>The interpretation I have from the <code>config.toml</code> is that we are not overriding the allocator to use <code>jemalloc</code>. We may be claiming that one <em>could</em> (by setting <code>rust.jemalloc</code> to true in the <code>config.toml</code>), and it sounds like that claim is incorrect (or at least, that is what you are trying to alert us about).</p>



<a name="225592798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/225592798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#225592798">(Feb 08 2021 at 19:11)</a>:</h4>
<p>(I need to review the jemallocator comment more carefully.)</p>



<a name="227356528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/227356528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#227356528">(Feb 22 2021 at 23:20)</a>:</h4>
<p>I filed <a href="https://github.com/rust-lang/rust/issues/82423">#82423</a> to track this issue.  From my testing, I believe it is correct that jemalloc is not working as expected on macos.</p>



<a name="227356579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/227356579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#227356579">(Feb 22 2021 at 23:21)</a>:</h4>
<p>I didn't think we were enabling it to be honest, but sounds like we are</p>



<a name="227356620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/247081-t-compiler/performance/topic/Using%20a%20non-system%20allocator%20for%20rustc%20on%20macOS/near/227356620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/247081-t-compiler/performance/topic/Using.20a.20non-system.20allocator.20for.20rustc.20on.20macOS.html#227356620">(Feb 22 2021 at 23:21)</a>:</h4>
<p>Yes, in ci.yml, it is explicitly enabled.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>